﻿package com.util {

	import flash.display.Sprite;
	import flash.net.URLRequest;
	import flash.text.engine.FontLookup;
	import flash.text.engine.Kerning;
	import flash.text.engine.RenderingMode;
	import flash.text.engine.FontWeight;
	
	//import flashx.textLayout.container.ContainerController;
	import flashx.textLayout.container.DisplayObjectContainerController;
	import flashx.textLayout.elements.Configuration;
	import flashx.textLayout.elements.ParagraphElement;
	import flashx.textLayout.elements.SpanElement;
	import flashx.textLayout.elements.TextFlow;
	import flashx.textLayout.formats.Direction;
	import flashx.textLayout.formats.CharacterFormat;
	import flashx.textLayout.formats.ParagraphFormat;
	//import flashx.textLayout.formats.TextLayoutFormat;	
	
	/**Format the textlayout */
	public class TextLayoutFormatter {
		
		
		
		public static var fontUrl:URLRequest;
		
		/**Static function format Textlayout
		 * @param SRC is for which sprite will genera put into the stage
		 * @param other is for format 
		 * @return a TextFlow object
		 * */		
		public static function format(src:Sprite, fwidth:Number, fheight:Number, fcolor:uint, fsize:Number, 
							   str:String, ffont:String = "Arial", fleading:Number = 0, fletterSpacing:Number = 0, 
							   fthickness:Number = 0, html:Boolean= true, funderline:Boolean = false, 
							   fbold:Boolean = false, falign:String = "left"):TextFlow 
		{	
			
			
			var charFormat:CharacterFormat = new CharacterFormat();
			var paraFormat:ParagraphFormat = new ParagraphFormat();
			
			//var textLayoutFormat:TextLayoutFormat = new TextLayoutFormat();
		
			charFormat.color= fcolor;
			charFormat.fontFamily = ffont;
			charFormat.fontSize = fsize;
			charFormat.kerning = Kerning.ON;
			charFormat.fontWeight = fbold ? "bold" : "normal";
			

			charFormat.fontLookup = flash.text.engine.FontLookup.EMBEDDED_CFF;
			charFormat.renderingMode = flash.text.engine.RenderingMode.CFF; 
			charFormat.cffHinting = flash.text.engine.CFFHinting.NONE;
			
			//the code below is for Device text use.
			//charFormat.fontLookup = flash.text.engine.FontLookup.DEVICE;
			//charFormat.renderingMode = flash.text.engine.RenderingMode.CFF; 
			//charFormat.cffHinting = flash.text.engine.CFFHinting.NONE;			
			//trace(charFormat.cffHinting +'===cffhint');
			var config:Configuration = new Configuration();
			config.textFlowInitialCharacterFormat=charFormat;			
			config.textFlowInitialParagraphFormat=paraFormat;

			var textFlow:TextFlow = new TextFlow(config);
			
			textFlow.textAlign = falign;			
			textFlow.direction = Direction.LTR;
			textFlow.fontLookup = flash.text.engine.FontLookup.EMBEDDED_CFF;
			textFlow.renderingMode = flash.text.engine.RenderingMode.CFF; 
			textFlow.cffHinting = flash.text.engine.CFFHinting.NONE;
			//textFlow.embedAsCFF = true;			
			var p:ParagraphElement = new ParagraphElement();
			var span:SpanElement = new SpanElement();
			
			span.text = str;		
			p.addChildAt(0,span);
			textFlow.addChildAt(0,p);			
			
			// NOTE: Is there a way not to input the height?			
			//textFlow.flowComposer.addController(new ContainerController(src,fwidth,fheight));
			//textFlow.flowComposer.updateAllControllers ()  ;	
			//fwidth=(fwidth<10)?str.length*8:fwidth;
			textFlow.flowComposer.addController(new DisplayObjectContainerController(src,fwidth,fheight));
			textFlow.flowComposer.updateAllContainers()  ;
					
			return textFlow;
			
		}
	}
}